<template>
  <el-dialog title="自定义内容" width="30%" :visible="visible" @close="visible = !visible">
    <el-form ref="elForm" :model="formData" :rules="rules">
      <el-form-item prop="content">
        <el-input
          type="textarea"
          :autosize="{ minRows: 6, maxRows: 8 }"
          v-model.trim="formData.content"
        />
      </el-form-item>
      <el-form-item style="text-align: center">
        <el-button @click="resetForm"> 取消 </el-button>
        <el-button type="primary" @click="submitForm"> 确认 </el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
export default {
  name: 'CustomPopup',
  props: {
    currentIndex: {
      type: [Number, String],
      default: ''
    },
    currentContent: {
      type: String,
      default: ''
    },
    dialogVisible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      formData: {
        content: ''
      },
      rules: {
        content: [
          {
            required: true,
            message: '自定义内容不能为空'
          },
          {
            max: 200,
            message: '不能超过200个字'
          }
        ]
      }
    }
  },
  computed: {
    visible: {
      get() {
        return this.dialogVisible
      },
      set(val) {
        this.$emit('update:dialogVisible', val)
      }
    },
    id() {
      return this.$route.query.id
    }
  },
  watch: {},
  created() {
    this.formData.content = this.currentContent ? this.currentContent : ''
  },
  mounted() {},
  methods: {
    submitForm() {
      this.$refs['elForm'].validate(valid => {
        if (!valid) return
        this.$emit('submit', { index: this.currentIndex, content: this.formData.content })
        this.visible = false
      })
    },
    resetForm() {
      this.visible = false
      this.$refs['elForm'].resetFields()
    }
  }
}
</script>

<style lang="scss" scoped></style>
